function MassPromise() {
    this.tasks = [];
    this.addTask = function (promise) {
        tasks.push(promise);
    };
    this.awaitAll = function () {
        console.log(`MassPromise.awaitContinues...${0}`);
        return awaitContinues(tasks, 0);
    }
    return this;
}

function awaitContinues(tasks, index) {
    let task = tasks[index];
    let promise = (typeof task == 'function') ? task() : task;
    return promise
        .then(() => {
            index += 1;
            if (index < tasks.length) {
                console.log(`MassPromise.awaitContinues.then...${index}`);
                return awaitContinues(tasks, index);
            }
            console.log(`MassPromise.awaitContinues.then...end`);
        }).catch(err => {
            console.log(err);
            index += 1;
            if (index < tasks.length) {
                console.log(`MassPromise.awaitContinues.catch...${index}`);
                return awaitContinues(tasks, index);
            }
            console.log(`MassPromise.awaitContinues.catch... end`);
        });
}

module.exports = MassPromise;